package org.dfl.plugin.editor;

import org.dfl.plugin.editor.tools.PanningSelectionToolWithDblClick;
import org.dfl.plugin.views.palette.PaletteListener;
import org.dfl.plugin.views.palette.PaletteView;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.Tool;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.ui.IEditorPart;


public class DflEditDomain extends DefaultEditDomain {

	/**
	 * Listens to the PaletteViewer for changes in selection, and sets the
	 * Domain's Tool accordingly.
	 */
	private PaletteListener paletteListener = new PaletteListener() {
		public void selectedToolChanged(Tool t) {
			setActiveTool(t);
		}
	};
	
	public DflEditDomain(IEditorPart editorPart) {
		super(editorPart);
		PaletteView.addPaletteListener(paletteListener);
	}

	@Override
	public void loadDefaultTool() {
		setActiveTool(getDefaultTool());
		PaletteView view = PaletteView.getView();
		if(view != null){
			view.selectDefaultEntry();
		}
	}
	
	@Override
	public void setActiveTool(Tool tool) {
		if(tool == null){
			loadDefaultTool();
		}else{
			super.setActiveTool(tool);
		}
	}
	
	@Override
	public Tool getDefaultTool() {
		return new PanningSelectionToolWithDblClick();
	}
	
	@Override
	public void setPaletteRoot(PaletteRoot root) {
	}

	@Override
	public void setPaletteViewer(PaletteViewer palette) {
		// no palette viewer! - all gets from PaletteView
	}
}
